//////////////////////////////////////////
//////////////////////////////////////////
//// This program reproduces TABLE 7  ////
//// fron Duan et al. (2020):         ////
//////////////////////////////////////////
//////////////////////////////////////////

clear
set more off
set type double
graph drop _all
program drop _all
set scheme s2mono

cd "\\file\Usersw$\wrr15\Home\My Documents\My Files\UCMETA\UC META-ANALYSIS WORKSHOP (March 2022)"
// log using ""Part 2 Results"
use "meta-orig.dta", clear

rename spill_other_firm all
rename idstudy id
rename sameregion spill_reg
rename sameindustry spill_ind
rename spill_exporter spill_ex
rename spill_value value
rename spill_employment employment
rename spill_output output
rename spill_number number
rename spill_asset asset
rename spill_rd rdsp
rename spill_other other_ms
rename olsgls ogls
rename probit plt
rename nonspherical nsph
rename endogeneity endg
rename categorical catg
rename sampleselection sps
rename panel_fe fe
rename aggregate agg
rename crosssection cs
rename labourquality labq
rename productivity prod
rename publicationyear pubyear

replace all = 1 if foreign ==1
drop foreign
rename all nondomestic
replace other_ms = 1 if asset==1
drop asset
replace impact = 0 if impact ==.
gen firm_level = 1 if agg == 0
replace firm_level = 0 if agg == 1

******************************************************************************
*DEFINITION OF BASIC VARIABLES
******************************************************************************
gen tstat = t
gen df = samplesize - k
gen pcc = tstat/sqrt(tstat^2+df)
gen varpcc = (1-pcc^2)/df
gen sepcc = sqrt(varpcc)
gen avgyear=(end+start)/2
gen tspan= end-start+1

******************************************************************************
* Kick out extreme values of PCC
******************************************************************************
quietly summ pcc, detail
keep if pcc > r(p1) & pcc < r(p99) 

******************************************************************************
* Calculating study weights
******************************************************************************
bysort id: egen numberests = count(id)

******************************************************************************
* Calculating std error for RE estimator
******************************************************************************
meta set pcc sepcc , random
meta regress _cons
scalar tau2 = e(tau2)
scalar list tau2 
// Can get the same estimate doing the following
gen seRE = sqrt(sepcc^2+tau2)

******************************************************************************
* Calculating the four weights
******************************************************************************
gen FEweight1 = 1/(sepcc^2)
gen REweight1 = 1/(seRE^2)
gen FEweight2 = 1/(numberests*sepcc^2)
gen REweight2 = 1/(numberests*seRE^2)


//////////////////////////////////////////////
//////////////  FIXED EFFECTS  ///////////////
//////////////////////////////////////////////

// THIS SECTION GIVES EQUAL WEIGHT TO EACH OBSERVATION

*------------------------------------------------------------*
*   TABLE 7: FixedEffects(Weight1) // All Control Variables  *
*------------------------------------------------------------*

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=FEweight1], vce(cluster id)

*----------------------------------------------------------*
*   TABLE 7: FixedEffects(Weight1) // Stepwise Regression  *
*----------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the se variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect pcc number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=FEweight1], backward bic ///
fix(sepcc spill_ex spill_reg spill_ind spill_fdi)

///////////////////////////////////////////////////////////////////////////////////
// NOTE: You first have to see which variables the stepwise procedure above selects
// before specifying the regression below
///////////////////////////////////////////////////////////////////////////////////

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi number ///
value employment avgyear firm_level  ///
china binary endg fe capital rd ///
published [aweight=FEweight1] , vce(cluster id) 

// THIS SECTION GIVES EQUAL WEIGHT TO EACH STUDY

*----------------------------------------------------------*
*   TABLE 7: FixedEffects(Weight2) // All Control Variables  *
*----------------------------------------------------------*

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=FEweight2], vce(cluster id)

*--------------------------------------------------------*
*   TABLE 7: FixedEffects(Weight2) // Stepwise Regression  *
*--------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the se variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect pcc number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=FEweight2], backward bic ///
fix(sepcc spill_ex spill_reg spill_ind spill_fdi) 

///////////////////////////////////////////////////////////////////////////////////
// NOTE: You first have to see which variables the stepwise procedure above selects
// before specifying the regression below
///////////////////////////////////////////////////////////////////////////////////

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi  ///
value employment avgyear firm_level  ///
china binary plt fe capital rd ///
published impact citation [aweight = FEweight2], vce(cluster id)

///////////////////////////////////////////////
//////////////  RANDOM EFFECTS  ///////////////
///////////////////////////////////////////////

///////////////////////////////////////////////////////
// THIS SECTION GIVES EQUAL WEIGHT TO EACH OBSERVATION
///////////////////////////////////////////////////////

*-----------------------------------------------------------*
*   TABLE 7: RandomEffects(Weight1) // All Control Variables  *
*-----------------------------------------------------------*

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=REweight1], vce(cluster id)

*---------------------------------------------------------*
*   TABLE 7: RandomEffects(Weight1) // Stepwise Regression  *
*---------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the se variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect pcc number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=REweight1], backward bic ///
fix(sepcc spill_ex spill_reg spill_ind spill_fdi) 

///////////////////////////////////////////////////////////////////////////////////
// NOTE: You first have to see which variables the stepwise procedure above selects
// before specifying the regression below
///////////////////////////////////////////////////////////////////////////////////

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi  ///
value employment avgyear domestic  ///
china plt sps fe labq capital rd ///
published impact [aweight=REweight1], ///
vce(cluster id)
// These results are slightly different from what is reported in the Duan paper

/////////////////////////////////////////////////
// THIS SECTION GIVES EQUAL WEIGHT TO EACH STUDY
/////////////////////////////////////////////////

*-----------------------------------------------------------*
*   TABLE 7: RandomEffects(Weight2) // All Control Variables  *
*-----------------------------------------------------------*

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=REweight2], vce(cluster id)

*---------------------------------------------------------*
*   TABLE 7: RandomEffects(Weight2) // Stepwise Regression  *
*---------------------------------------------------------*

// Here we use backwards stepwise regression selecting on the best regression using the BIC criterion
// We lock in the 4 spillover variables and the cse variable: Spill_ind Spill_reg Spill_FDI Spill_ex 
vselect pcc number ///
value employment manufacturing avgyear firm_level domestic oecd ///
china binary plt endg sps fe size prod labq capital rd ///
published impact citation [aweight=REweight2], backward bic ///
fix(sepcc spill_ex spill_reg spill_ind spill_fdi) 

///////////////////////////////////////////////////////////////////////////////////
// NOTE: You first have to see which variables the stepwise procedure above selects
// before specifying the regression below
///////////////////////////////////////////////////////////////////////////////////

reg pcc sepcc spill_ex spill_reg spill_ind spill_fdi number ///
value employment avgyear  ///
china plt sps fe prod labq capital rd ///
impact  [aweight=REweight2], vce(cluster id)


// log close

 